package com.ligong.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ligong.blog.common.util.BeanTransformUtils;
import com.ligong.blog.contanst.Constants;
import com.ligong.blog.mapper.BlogInfoMapper;
import com.ligong.blog.pojo.dataobject.BlogInfo;
import com.ligong.blog.pojo.request.AddBlogRequest;
import com.ligong.blog.pojo.request.UpdateBlogRequest;
import com.ligong.blog.pojo.response.BlogDetailResponse;
import com.ligong.blog.pojo.response.BlogInfoResponse;
import com.ligong.blog.service.BlogService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service("blogService")
public class BlogServiceImpl implements BlogService {

    @Resource(name = "blogInfoMapper")
    private BlogInfoMapper blogInfoMapper;

    @Override
    public List<BlogInfoResponse> getList() {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag, Constants.NOT_DELETE);
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(queryWrapper);

        List<BlogInfoResponse> blogInfoResponseList = blogInfos.stream().map(blogInfo -> {
            BlogInfoResponse blogInfoResponse = new BlogInfoResponse();
            blogInfoResponse.setId(blogInfo.getId());
            BeanUtils.copyProperties(blogInfo, blogInfoResponse);
            return blogInfoResponse;
        }).collect(Collectors.toList());
        return blogInfoResponseList;
    }

    @Override
    public BlogDetailResponse getDetail(Integer blogId) {
        BlogInfo blogInfo = blogInfoMapper.selectById(blogId);
        BlogDetailResponse blogDetailResponse = new BlogDetailResponse();
        BeanUtils.copyProperties(blogInfo, blogDetailResponse);
        return blogDetailResponse;
    }

    @Override
    public Boolean addBlog(AddBlogRequest addBlogRequest) {
        BlogInfo blogInfo = BeanTransformUtils.trans(addBlogRequest);
        try {
            Integer ret = insertBlog(blogInfo);
            return ret == 1;
        } catch (Exception e) {
            log.error("发布博客失败, e:" + e);
        }
        return false;
    }

    @Override
    public Boolean updateBlog(UpdateBlogRequest request) {
        BlogInfo blogInfo = new BlogInfo();
        BeanUtils.copyProperties(request, blogInfo);
        try {
            Integer result = this.update(blogInfo);
            return result == 1;
        } catch (Exception e) {
            log.error("更新博客失败, e: " + e);
        }
        return false;
    }

    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(Constants.IS_DELETE);
        try {
            Integer result = this.update(blogInfo);
            return result == 1;
        } catch (Exception e) {
            log.error("删除博客失败, e: " + e);
        }
        return false;
    }


    public Integer insertBlog(BlogInfo blogInfo) {
        return blogInfoMapper.insert(blogInfo);
    }

    public Integer update(BlogInfo blogInfo) {
        return blogInfoMapper.updateById(blogInfo);
    }


}
